package com.kky.pp.activity.login.helper

import android.app.Activity
import android.os.Build
import android.os.Bundle
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.kky.pp.App
import com.kky.pp.common.Constant
import com.kky.pp.base.RegisterConfigBean
import com.kky.pp.entity.ResultDataEntity
import com.kky.pp.activity.login.LoginActivity
import com.kky.pp.activity.login.phone.PhoneLoginActivity
import com.kky.pp.activity.register.RegisterActivity
import io.rong.imkit.SpName
import io.rong.imkit.utils.JsonUtils
import org.json.JSONObject

class LoginRegisterHelper {
    //avtivity传递参数KEY
    val JUMP_PARAMETER_PASSING_KEY = "jump_parameter_passing"

    //注册配置数据
    var bean: RegisterConfigBean? = null


    fun initData(activity: RegisterActivity, result: ((bean: RegisterConfigBean) -> Unit?)?) {
        bean = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            activity.intent.getSerializableExtra(
                JUMP_PARAMETER_PASSING_KEY,
                RegisterConfigBean::class.java
            )
        } else {
            val registerConfigBean = activity.intent.getSerializableExtra("entity")
            if (registerConfigBean == null){
                null
            }else{
                registerConfigBean as RegisterConfigBean
            }
        }
        if (bean != null) {
            result?.invoke(bean!!)
        } else {
            requestRegisterConfig {
                if (bean != null) {
                    result?.invoke(bean!!)
                }
            }
        }
    }

    fun upAddProfile(activity: Activity) {
        if (bean != null) {
            //bean存在数据直接跳转AddProfileActivity
            up(activity)
            return
        }
        //bean不存在数据，尝试请求
        requestRegisterConfig {
            if (bean != null) {
                //bean存在数据直接跳转AddProfileActivity
                up(activity)
            }
        }
    }

    private fun up(activity: Activity) {
        val bundle = Bundle()
        bundle.putSerializable(JUMP_PARAMETER_PASSING_KEY, bean)
        if (activity is LoginActivity) {
            activity.startActivity(RegisterActivity::class.java, bundle)
        } else if (activity is PhoneLoginActivity) {
            activity.startActivity(RegisterActivity::class.java, bundle)
        }
        activity.finish()
    }


    /**
     * 请求获取注册配置
     */
    fun requestRegisterConfig(result: (() -> Unit?)? = null) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(Constant.register_config_url)
            }
        }, object : SDOkHttpResoutCallBack<ResultDataEntity<RegisterConfigBean>>() {
            override fun onSuccess(entity: ResultDataEntity<RegisterConfigBean>) {
                if (entity.data == null)
                    return
                bean = entity.data
                val imageList = arrayListOf<String>()
                for (i in 0 until entity.data.configs.size) {
                    if (entity.data.configs[i].backgroundPhoto != null) {
                        if (entity.data.configs[i].backgroundPhoto.contains("|")) {
                            val split = entity.data.configs[i].backgroundPhoto.split("|")
                            imageList.addAll(split)
                        } else {
                            imageList.add(entity.data.configs[i].backgroundPhoto)
                        }
                    }
                }
                for (i in 0 until imageList.size) {
                    App.app?.applicationContext?.let {
                        Glide.with(it)
                            .load(imageList[i])
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .preload()
                    }
                }
            }

            override fun onFinish() {
                result?.invoke()
                super.onFinish()
            }
        })
    }

    fun isNaturalFlow(): Boolean {
        val channel = BaseConfig.getInstance.getString(SpName.channel, "")
        return if (channel != "" && JsonUtils.isJSON(channel)) {
            val jsonObject = JSONObject(channel)
            if (jsonObject.has("af_status")) {
                val afStatus = jsonObject.getString("af_status")
                return afStatus.equals("Organic", true)
            } else {
                return true
            }
        } else {
            return true
        }
    }

}